﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My.DataAccess.Core;
using NCLCustomerManagement.DataTransferObjects.Sale;

namespace NCLCustomerManagement.DataAccess.Sale
{
    public class SaleRepository : BaseRepository<SaleDTO, DailySale>, ISaleRepository
    {
        public IEnumerable<SaleDTO> GetSaleList(DateTime fromDate, DateTime toDate, string customerPhone, int? productId = null, int? categoryId = null)
        {
            var list = UnitOfWork.Table<DailySale>()
                .Where(e => (e.SaleDate.Date >= fromDate.Date && e.SaleDate.Date <= toDate.Date))
                .AsEnumerable();
            if (!string.IsNullOrEmpty(customerPhone))
            {
                list = list.Where(e => e.Customer.Phone == customerPhone).AsEnumerable();
            }
            if (categoryId.HasValue)
            {
                list = list.Where(e => e.Product.ProductCategoryId == categoryId.Value);
            }
            if (productId.HasValue)
            {
                list = list.Where(e => e.ProductId == productId.Value);
            }
            return list.Select(e => ConvertSale(e));
        }

        public SaleDTO ConvertSale(DailySale entity)
        {
            return new SaleDTO()
            {
                Id = entity.Id,
                CustomerName = entity.Customer.CustomerName,
                Discount = entity.Discount,
                ProductName = entity.Product.ProductName,
                Quantity = entity.Quantity,
                SaleDate = entity.SaleDate,
                Tax = entity.Tax,
                Unit = entity.Product.PrimaryUnit,
                UnitPrice = entity.SaleUnitPrice,
                CustomerId = entity.CustomerId,
                ProductId = entity.ProductId,
                AvailableDayUsed = entity.AvailableDayUsed,
                CreatedBy = entity.CreatedBy

            };
        }

        public DailySale ConvertSale(SaleDTO dto)
        {
            return new DailySale()
            {
                Id = dto.Id,
                CustomerId = dto.CustomerId,
                ProductId = dto.ProductId,

                Discount = dto.Discount,

                Quantity = dto.Quantity,
                SaleDate = dto.SaleDate,
                Tax = dto.Tax,

                SaleUnitPrice = dto.UnitPrice,
                Notes = dto.Notes,
                CreatedBy = dto.CreatedBy,
                AvailableDayUsed = dto.AvailableDayUsed

            };
        }



        public void AddSaleItem(SaleDTO dto)
        {
            var product = UnitOfWork.Table<Product>().SingleOrDefault(e => e.Id == dto.ProductId);

            var entity = ConvertSale(dto);
            entity.AvailableDayUsed = entity.Quantity * (product.QuantityPerUnit / product.UsedPerDay);

            UnitOfWork.InsertOnSubmit<DailySale>(entity);
            UnitOfWork.SubmitChanges();
        }

        public void DeleteSaleItem(Guid id)
        {
            var entity = UnitOfWork.Table<DailySale>().SingleOrDefault(e => e.Id == id);
            if (entity != null)
            {
                UnitOfWork.DeleteOnSubmit<DailySale>(entity);
                UnitOfWork.SubmitChanges();
            }
        }


        public SaleDTO GetSaleItem(Guid id)
        {
            var entity= UnitOfWork.Table<DailySale>().SingleOrDefault(e => e.Id == id);
            if (entity != null)
                return ConvertSale(entity);
            return null;
        }
    }
}
